package com.cs.pk.utils;

import java.io.IOException;
import java.io.InputStream;

public class CmdInputStreamReader extends Thread {
	private InputStream is = null;
	// private StringWriter sw;
	private StringBuffer sb = null;
	private String inputType = null; 
	
	private boolean printLog = false;

	private boolean startCaching = true;
    
	

	public CmdInputStreamReader(InputStream is,String inputType) {
		this(is,inputType,false);
	}
	
	public CmdInputStreamReader(InputStream is,String inputType,boolean printLog) {
		this(is,inputType,printLog,true);
	}
	
	public CmdInputStreamReader(InputStream is,String inputType,boolean printLog,boolean startCaching) {
		this.is = is;
		this.printLog = printLog;
		sb = new StringBuffer();
		this.inputType = inputType;
		this.startCaching = startCaching;
	}


	public void run() {
		try {
			byte b[] = new byte[128];
			int n = 0;
			String output = null ;
			int count = 0;
			while ((n = is.read(b)) != -1) {
				output = new String(b, 0, n);
				if(printLog) {
				   System.out.println(output);
				}

				if(startCaching) {				
					sb.append(output);
				}
				b = new byte[128];
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getResult() {
		return sb.toString();
	}

	public void dispose() throws IOException {
		if (is != null) {
			is.close();				
		}
		is = null;
		sb = null;
	}
	
	public void setPrintLog(boolean printLog) {
		this.printLog = printLog;
	}
	
	public boolean isStartCaching() {
		return startCaching;
	}

	public void setStartCaching(boolean startCaching) {
		this.startCaching = startCaching;
	}
	
}

